//
//  WBWelcomView.swift
//  传智微博
//
//  Created by zhoujiming on 2017/2/10.
//  Copyright © 2017年 gmcc. All rights reserved.
//

import UIKit
import SDWebImage


// 欢迎试图
class WBWelcomView: UIView {
    
    
    @IBOutlet weak var iconView: UIImageView!
    
    @IBOutlet weak var tipLabel: UILabel!
    
    @IBOutlet weak var bottomCons: NSLayoutConstraint!
    
    
    @IBOutlet weak var iconWidthCons: NSLayoutConstraint!
    
    class func welcomeView()->WBWelcomView{
        
        let nib = UINib(nibName: "WBWelcomView", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBWelcomView
        
        v.frame = UIScreen.main.bounds
        return v
    }
    
    // 从xib加载完成调用
    override func awakeFromNib() {
        guard let urlString = WBNetworkManager.shared().userAccount.avatar_large,
            let url = URL(string: urlString) else{
            return
        }
        
        iconView.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
        
        
        // 设置圆角
        iconView.layer.cornerRadius = iconWidthCons.constant * 0.5// iconView.bounds.width * 0.5
        iconView.layer.masksToBounds = true
    }
    
    /// 视图被添加到window上，表示视图已经被显示
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        // 更新约束
        self.layoutIfNeeded()
        
        bottomCons.constant = bounds.size.height - 200
        
        UIView.animate(withDuration: 1,
                       delay: 0,
                       usingSpringWithDamping: 0.7,
                       initialSpringVelocity: 0,
                       options: [],
                       animations: {
                        // 更新约束
                        self.layoutIfNeeded()
        }){(_) in
            
            UIView.animate(withDuration: 1, animations: {
                self.tipLabel.alpha = 1
            }, completion: { (_) in
                super.removeFromSuperview()
            })
        }
    }

}
